﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;

namespace LevenBlog.Data.SqlServer
{
    public sealed class SqlUser : IUser
    {
        public int Count(IDbProvider provider)
        {
            string sql = "select count([id]) from [lb_user]";
            return Convert.ToInt32(SqlHelper.Instance.ExecuteScalar(provider, sql));
        }

        public int CountForSearch(IDbProvider provider, string searchName)
        {
            string sql = string.Format("select count([id]) from [lb_user] where [name] like '%{0}%'", searchName);
            return Convert.ToInt32(SqlHelper.Instance.ExecuteScalar(provider, sql));
        }

        public int CountForTypeID(IDbProvider provider, int typeid)
        {
            string sql = string.Format("select count([id]) from [lb_user] where [typeid]={0}", typeid);
            return Convert.ToInt32(SqlHelper.Instance.ExecuteScalar(provider, sql));
        }

        public int Insert(IDbProvider provider, lb_user user)
        {
            IDataParameter[] paras = null;
            string sql = SqlHelper.Instance.CreateInsertSql(user, out paras);
            sql += ";select @@identity;";
            return Convert.ToInt32(SqlHelper.Instance.ExecuteScalar(provider, sql, paras));
        }

        public void Update(IDbProvider provider, lb_user user)
        {
            IDataParameter[] paras = null;
            string sql = SqlHelper.Instance.CreateUpdateSql(user, out paras);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Delete(IDbProvider provider, int id)
        {
            string sql = string.Format("delete from [lb_user] where [id]={0}", id);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public void Delete(IDbProvider provider, string name)
        {
            string sql = string.Format("delete from [lb_user] where [name]='{0}'", name);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public lb_user Get(IDbProvider provider, int id)
        {
            string sql = string.Format("select * from [lb_user] where [id]={0}", id);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToEntity<lb_user>(dr);
            }
        }

        public lb_user Get(IDbProvider provider, string name)
        {
            string sql = string.Format("select * from [lb_user] where [name]='{0}'", name);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToEntity<lb_user>(dr);
            }
        }

        public IList<lb_user> Get(IDbProvider provider, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_user]", string.Empty, "[id]", "order by [id] desc");
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToList<lb_user>(dr);
            }
        }

        public IList<lb_user> GetFromTypeID(IDbProvider provider, int typeid)
        {
            string sql = string.Format("select * from [lb_user] where [typeid]={0}", typeid);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToList<lb_user>(dr);
            }
        }

        public IList<lb_user> Get(IDbProvider provider, int typeid, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_user]", string.Empty, "[id]", "order by [id] desc");
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToList<lb_user>(dr);
            }
        }

        public IList<lb_user> Search(IDbProvider provider, string searchName, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_user]", string.Format("[name] like '%{0}%'", searchName), "[id]", "order by [id] desc");
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToList<lb_user>(dr);
            }
        }

        public IList<lb_user> GetAll(IDbProvider provider, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*,b.id as infoid", "[lb_user] as a left join [lb_userinfo] as b on b.userid=a.id", string.Empty, "a.id", "order by a.id desc");
            return ReadAll(provider, sql);
        }

        public IList<lb_user> SearchAll(IDbProvider provider, string searchName, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*,b.id as infoid", "[lb_user] as a left join [lb_userinfo] as b on b.userid=a.id", string.Format("a.name like '%{0}%'", searchName), "a.id", "order by a.id desc");
            return ReadAll(provider, sql);
        }

        private IList<lb_user> ReadAll(IDbProvider provider, string sql)
        {
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                IList<lb_user> result = new List<lb_user>();
                lb_user user = null;
                while (dr.Read())
                {
                    user = new lb_user();
                    user.id = Convert.ToInt32(dr["id"]);
                    user.name = Convert.ToString(dr["name"]);
                    user.password = Convert.ToString(dr["password"]);
                    user.postnum = Convert.ToInt32(dr["postnum"]);
                    user.typeid = Convert.ToInt32(dr["typeid"]);
                    user.userinfo = new lb_userinfo();
                    user.userinfo.answer = Convert.ToString(dr["answer"]);
                    user.userinfo.birthday = Convert.ToDateTime(dr["birthday"]);
                    user.userinfo.face = Convert.ToString(dr["face"]);
                    user.userinfo.id = Convert.ToInt32(dr["infoid"]);
                    user.userinfo.lastloginip = Convert.ToString(dr["lastloginip"]);
                    user.userinfo.lastlogintime = Convert.ToDateTime(dr["lastlogintime"]);
                    user.userinfo.mail = Convert.ToString(dr["mail"]);
                    user.userinfo.nickname = Convert.ToString(dr["nickname"]);
                    user.userinfo.question = Convert.ToString(dr["question"]);
                    user.userinfo.regip = Convert.ToString(dr["regip"]);
                    user.userinfo.regtime = Convert.ToDateTime(dr["regtime"]);
                    user.userinfo.sex = Convert.ToInt16(dr["sex"]);
                    user.userinfo.signature = Convert.ToString(dr["signature"]);
                    user.userinfo.userid = Convert.ToInt32(dr["userid"]);
                    result.Add(user);
                }
                return result;
            }
        }

        public IList<string> GetMail(IDbProvider provider, int[] idArray)
        {
            if (idArray.Length < 1)
                return new List<string>();
            StringBuilder sb = new StringBuilder("select a.id,a.name,b.mail from [lb_user] as a left join [lb_userinfo] as b on b.userid=a.id where ");
            foreach (int item in idArray)
            {
                sb.AppendFormat("a.id={0} or ", item);
            }
            sb.Remove(sb.Length - 4, 4);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sb.ToString()))
            {
                IList<string> result = new List<string>();
                while (dr.Read())
                {
                    result.Add(Convert.ToString(dr["mail"]));
                }
                return result;
            }
        }
    }
}
